﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace 闹钟
{
    class Program
    {
        class Clock
        {
            public delegate void clockFunction();
            public event clockFunction Tick;
            public delegate void clockFunction2(Clock clock);
            public event clockFunction2 Alarm;

            private DateTime datetime = DateTime.Now;
            static bool judge = true;
            public int hour;
            public int min;
            public int sec;

            public void runAlarm()
            {
                while (judge)
                {
                    Thread.Sleep(1000);
                    Console.WriteLine(Convert.ToString(DateTime.Now));
                    this.Tick();
                    this.Alarm(this);
                }
            }

            public void setAlarm()
            {
                Console.WriteLine("请设置闹钟的时：");
                hour = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请设置闹钟的分：");
                min = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请设置闹钟的秒：");
                sec = Convert.ToInt32(Console.ReadLine());
            }

            public void Ring(Clock clock)
            {
                if ((DateTime.Now.Hour == clock.hour) && (DateTime.Now.Minute == clock.min)
                    &&(DateTime.Now.Second==clock.sec))
                {
                    Console.WriteLine("闹钟时间到！");
                    judge = false;
                }
            }

            public void tick()
            {
                Console.WriteLine("嘀嗒");
            }
        }

        static void Main(string[] args)
        {
            Clock clock = new Clock();
            clock.Tick += clock.tick;
            clock.Alarm += clock.Ring;

            while (true)
            {

                Console.WriteLine("请输入要选择的功能：\n1.设置一个新闹钟 \n2.开始闹钟运行");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        clock.setAlarm();
                        break;
                    case 2:
                        clock.runAlarm();
                        break;
                    default:
                        Console.WriteLine("输入错误，请重新输入！");
                        break;
                }
            }
        }
    }
}
